package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.pojo.UserDynamicComment;
import com.example.vo.user.Publisher;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
 * @author LeXin Huang
 * @date 2021年03月29日 16:19
 */
@Mapper
public interface UserDynamicCommentMapper extends BaseMapper<UserDynamicComment> {

    @Results(id = "commentMap", value = {
            @Result(column = "id", property = "id", javaType = Integer.class, id = true),
            @Result(column = "dynamic_comment_content", property = "content", javaType = String.class),
            @Result(column = "dynamic_comment_time", property = "commentTime", javaType = Date.class),
            @Result(column = "dynamic_id", property = "dynamicId", javaType = Integer.class),
            @Result(column = "user_id", property = "userId", javaType = Integer.class),
            @Result(column = "user_id", property = "commenter", javaType = Publisher.class, one = @One(
                    select = "com.example.mapper.PublisherMapper.selectById"
            ))
    })
    @Select(("Select * from user_dynamic_comment main_table INNER JOIN " +
            "(SELECT id from user_dynamic_comment where id < #{offset_id} and dynamic_id = #{dynamic_id} " +
            "order by id DESC LIMIT #{size}) temp_table " +
            "on main_table.id = temp_table.id order by main_table.id DESC, main_table.dynamic_comment_time DESC"))
    List<UserDynamicComment> selectHistoryComments(@Param("dynamic_id") Integer dynamicId,
                                                   @Param("offset_id") Integer offsetId,
                                                   @Param("size") Integer size);
}
